package com.carlinfo.demo.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.DbUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IADynastyDao;
import com.carlinfo.demo.pojo.ADynasty;

public class ADynastyDaoImpl extends BaseDaoImpl<ADynasty> implements IADynastyDao
{
	
	ADynasty dynasty = new ADynasty();
	@Override
	public int saveOneDao(ADynasty dynasty)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "insert into a_dynasty value(null,?,?,?,?,?,?,?,?,?)";
		paramsList.add (dynasty.getName());
		paramsList.add (dynasty.getCapital());
		paramsList.add (dynasty.getAge());
		paramsList.add (dynasty.getSt());
		paramsList.add (dynasty.getContext());
		paramsList.add (dynasty.getStatus());
		paramsList.add (dynasty.getCreateTime());
		paramsList.add (dynasty.getUpdateTime());
		paramsList.add (dynasty.getPubTime());
		this.pojoObj = dynasty;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int res = this.executeJDBC(sql, paramsList, resultMap);
		dynasty.setId(Integer.valueOf(resultMap.get("id")+""));
		return res;
	}

	@Override
	public int updateOneDao(ADynasty dynasty)
	{
		List<Object> paramsList = new ArrayList<Object>();
		/**
		 * 修改的时候应该先查出来
		 */
		String sql = "update a_dynasty set name = ?,capital = ?,st = ?,context = ?,"
				+ "age = ?,status = ?,createtime = ?,updatetime = ?,pubtime = ? where id = ?;";
		paramsList.add(dynasty.getName());
		paramsList.add(dynasty.getCapital());
		paramsList.add(dynasty.getSt());
		paramsList.add(dynasty.getContext());
		paramsList.add(dynasty.getAge());
		paramsList.add(dynasty.getStatus());
		paramsList.add(dynasty.getCreateTime());
		paramsList.add(dynasty.getUpdateTime());
		paramsList.add(dynasty.getPubTime());
		paramsList.add(dynasty.getId());
		this.pojoObj = dynasty;
		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "delete from a_dynasty ";
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			sql = sql + "where id = ?";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			sql = sql + "where name = ?";
			paramsList.add(condMap.get("name"));
		}
		this.pojoObj = dynasty;
		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public ADynasty selectOneDao(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "select * from a_dynasty ";
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			sql = sql + " where id = ? ";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}	 
		this.pojoObj = dynasty;
		return this.findOneJDBC(sql, paramsList);
	}

	/*protected ADynasty converFromDb(ResultSet res) throws SQLException
	{
		int id = res.getInt("id");
		String name = res.getString("name");
		String capital = res.getString("capital");
		String context = res.getString("context");
		int age = res.getInt("age");
		byte status = res.getByte("status");
		Date st = res.getDate("st");
		Date createTime = res.getTimestamp("createTime");
		Date updateTime = res.getTimestamp("updateTime");
		Date pubTime = res.getTimestamp("pubTime");

		ADynasty dynasty = new ADynasty();
		dynasty.setId(id);
		dynasty.setName(name);
		dynasty.setContext(context);
		dynasty.setCapital(capital);
		dynasty.setSt(st);
		dynasty.setAge(age);
		dynasty.setStatus(status);
		dynasty.setCreateTime(createTime);
		dynasty.setUpdateTime(updateTime);
		dynasty.setPubTime(pubTime);
		return dynasty;
	}*/

	@Override
	public List<ADynasty> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		List<ADynasty> dynastyList = new ArrayList<ADynasty>();
		String sql = "select * from a_dynasty where 1=1 ";
		if (!"".equalsIgnoreCase(condMap.get("keyword") + "") && condMap.get("keyword") != null)
		{
			sql += " and (name like ? or capital like ? or context like ?) ";
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			paramsList.add(condMap.get("status"));
		}
		if (condMap.get("stDate") instanceof Date && condMap.get("enDate") instanceof Date)
		{
			sql += " and (pubTime >= ? and pubTime <= ? )";
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("enDate"));
		}
		/* 排序 */
		if("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc";
		}else if("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc, name desc";
		}else
		{
			/* 默认的效果 */
			sql += " order by pubTime desc";
		}
		this.pojoObj = dynasty;
		if(pageInfoUtil != null)
		{
			return this.findListByPage(pageInfoUtil, sql, paramsList);
		}
		return this.findList(sql, paramsList);
	}

}
